package com.yjc.drygoods.member.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yjc.common.to.SkuInfoTo;
import com.yjc.common.vo.MemberRespVo;
import com.yjc.drygoods.member.entity.MemberCollectSkuEntity;
import com.yjc.drygoods.member.feign.ProductFeignService;
import com.yjc.drygoods.member.interceptor.LoginUserInterceptor;
import com.yjc.drygoods.member.service.MemberCollectSkuService;
import com.yjc.common.vo.ConsumeAttentionCountRelationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 会员收藏的商品
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 23:16:26
 */
@Slf4j
@RestController
@RequestMapping("member/membercollectsku")
public class MemberCollectSkuController {
    @Autowired
    private MemberCollectSkuService memberCollectSkuService;

    @Autowired
    ProductFeignService productFeignService;

    @GetMapping("/get/all/collect/skuInfo")
    public R getAllCollectSkuInfo() {
        List<MemberCollectSkuEntity> list = memberCollectSkuService.list();
        return R.ok().setData(list);
    }

    @GetMapping("/get/comnsume/attentionCount/relation")
    public R getComnsumeAttentionCountRelation() {
        List<ConsumeAttentionCountRelationVo> consumeAttentionCountRelations = memberCollectSkuService.getComnsumeAttentionCountRelation();
        return R.ok().setData(consumeAttentionCountRelations);
    }

    /**
     * 保存
     */
    @PostMapping("/save/attention")
    public R saveAttention(@RequestBody MemberCollectSkuEntity memberCollectSku) {
        log.info("MemberCollectSkuEntity:{}", memberCollectSku);
        MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();
        log.info("memberRespVo:{}", memberRespVo);
        memberCollectSku.setMemberId(memberRespVo.getId());
        memberCollectSku.setMemberName(memberRespVo.getUsername());
        memberCollectSku.setCreateTime(new Date());
        memberCollectSkuService.saveOrUpdate(memberCollectSku,
                new UpdateWrapper<MemberCollectSkuEntity>()
                        .eq("sku_id", memberCollectSku.getSkuId())
                        .eq("member_id", memberRespVo.getId()));
        SkuInfoTo skuInfoTo = new SkuInfoTo();
        Map<String, Object> map = new HashMap<>();
        map.put("skuId", memberCollectSku.getSkuId());
        map.put("flag", true);
        productFeignService.updateAttentionCount(map);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberCollectSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(@RequestParam("id") Long id) {
        MemberCollectSkuEntity memberCollectSku = memberCollectSkuService.getById(id);

        return R.ok().put("memberCollectSku", memberCollectSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberCollectSkuEntity memberCollectSku) {
        memberCollectSkuService.save(memberCollectSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberCollectSkuEntity memberCollectSku) {
        memberCollectSkuService.updateById(memberCollectSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberCollectSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
